<?php
session_start();
 $hostname = "localhost";
 $username = "root";
 $password = "";
 $dbname = "smkn_1_bandung";
 mysql_connect($hostname, $username, $password) or die('Could not connect to database: ' . mysql_error());
 mysql_select_db($dbname);
 
 $error = "";
 
 if (isset($_SESSION['role'])) {
	if ($_SESSION['role'] == "Guru") {
		// redirect ke page guru
		header('Location: guruMatPel_index.php');
	} else if ($_SESSION['role'] == "Staff Tata Usaha") {
		// redirect ke page tata usaha
		header('Location: tatausaha_index.php');
	} else if ($_SESSION['role'] == "Admin") {
		// redirect ke page admin
		header('Location: admin_index.php');
	} else if ($_SESSION['role'] == "Wali Kelas") {
		// redirect ke page wali kelas
		header('Location: walikelas_index.php');
	} else if ($_SESSION['role'] == "Siswa") {
		// redirect ke page siswa
		header('Location: siswa_index.php');
	}
}
 if (isset($_POST['username']) && isset($_POST['password'])) {
	//cek ke database
	$user = $_POST['username'];
	$pass = $_POST['password'];
	$query_admin = "SELECT * FROM admin WHERE user_name='{$user}' AND password='{$pass}'";
	$result_admin = mysql_query($query_admin);
	$query_guru = "SELECT * FROM guru WHERE user_name='{$user}' AND password='{$pass}'";
	$result_guru = mysql_query($query_guru);
	$query_siswa = "SELECT * FROM siswa WHERE user_name='{$user}' AND password='{$pass}'";
	$result_siswa = mysql_query($query_siswa);
	//kalau benar, redirect ke page admin
	if ((mysql_num_rows($result_admin) == 0)) {
		$error = "Username/password salah.";
	} else {
		$data = mysql_fetch_assoc($result_admin);

		$_SESSION['role'] = $data['role'];
		$_SESSION['user_name'] = $data['user_name'];
		$_SESSION['id_pengguna'] = $data['id'];

		if ($_SESSION['role'] == "Guru") {
			// redirect ke page guru
			header('Location: guruMatPel_index.php');
		} else if ($_SESSION['role'] == "Staff Tata Usaha") {
			// redirect ke page staf tata usaha
			header('Location: tatausaha_index.php');
		} else if ($_SESSION['role'] == "Admin") {
			// redirect ke page admin
			header('Location: admin_index.php');
		}else if ($_SESSION['role'] == "Wali Kelas") {
			// redirect ke page wali kelas
			header('Location: walikelas_index.php');
		}else if ($_SESSION['role'] == "Siswa") {
			// redirect ke page siswa
			header('Location: siswa_index.php');
		}
	}

	if ((mysql_num_rows($result_guru) == 0)) {
		$error = "Username/password salah.";
	} else {
		$data = mysql_fetch_assoc($result_guru);

		$_SESSION['role'] = $data['role'];
		$_SESSION['user_name'] = $data['user_name'];
		$_SESSION['id_pengguna'] = $data['NIK'];

		if ($_SESSION['role'] == "Guru") {
			// redirect ke page guru
			header('Location: guruMatPel_index.php');
		} else if ($_SESSION['role'] == "Staff Tata Usaha") {
			// redirect ke page staf tata usaha
			header('Location: tatausaha_index.php');
		} else if ($_SESSION['role'] == "Admin") {
			// redirect ke page admin
			header('Location: admin_index.php');
		}else if ($_SESSION['role'] == "Wali Kelas") {
			// redirect ke page wali kelas
			header('Location: walikelas_index.php');
		}else if ($_SESSION['role'] == "Siswa") {
			// redirect ke page siswa
			header('Location: siswa_index.php');
		}
	}

	if ((mysql_num_rows($result_siswa) == 0)) {
		$error = "Username/password salah.";
	} else {
		$data = mysql_fetch_assoc($result_siswa);

		$_SESSION['role'] = $data['role'];
		$_SESSION['user_name'] = $data['user_name'];
		$_SESSION['id_pengguna'] = $data['NIS'];

		if ($_SESSION['role'] == "Guru") {
			// redirect ke page guru
			header('Location: guruMatPel_index.php');
		} else if ($_SESSION['role'] == "Staff Tata Usaha") {
			// redirect ke page staf tata usaha
			header('Location: tatausaha_index.php');
		} else if ($_SESSION['role'] == "Admin") {
			// redirect ke page admin
			header('Location: admin_index.php');
		}else if ($_SESSION['role'] == "Wali Kelas") {
			// redirect ke page wali kelas
			header('Location: walikelas_index.php');
		}else if ($_SESSION['role'] == "Siswa") {
			// redirect ke page siswa
			header('Location: siswa_index.php');
		}
	}
} 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Portal SMKN 1 Bandung</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="ddaccordion.js"></script>
<script type="text/javascript">
ddaccordion.init({
	headerclass: "submenuheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["suffix", "<img src='images/plus.gif' class='statusicon' />", "<img src='images/minus.gif' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})
</script>

<script type="text/javascript" src="jconfirmaction.jquery.js"></script>
<script type="text/javascript">
	
	$(document).ready(function() {
		$('.ask').jConfirmAction();
	});
	
</script>

<script language="javascript" type="text/javascript" src="niceforms.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="niceforms-default.css" />

</head>
<body>
<div id="main_container">

	<div class="header_login">
   		<div class="logo"><img src="../prosi/images/header.png" title="" border="0" /></div>
    </div>
         <div class="login_form">
         <h1>LOGIN</h1>
         <form action="" method="post" class="niceform">

                <fieldset>
                    <dl>
                        <dt><label for="email">Nama Pengguna:</label></dt>
                        <dd><input type="text" name="username" id="" size="45" /></dd>
                    </dl>
                    <dl>
                        <dt><label for="password">Kata Sandi:</label></dt>
                        <dd><input type="password" name="password" id="" size="45" /></dd>
                    </dl>
                    <dl>
                    	<span style="margin-left:50px;color:#FF0000;"><?php echo $error;?></span>
                    </dl>
                     <dl class="submit">
                    <input type="submit" name="submit"  id="submit" value="Login" />
                     </dl>
                    
                </fieldset>
                
         </form>
    </div>  
    
    <?php include 'footerLogin.php'; ?>

</div>		
</body>
</html>